<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>The parallel\Future class</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="parallel-runtime.kill.html">« parallel\Runtime::kill</a></li>
      <li style="float: right;"><a href="parallel-future.cancel.html">parallel\Future::cancel »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="book.parallel.html">parallel</a></li>
    <li>The parallel\Future class</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="class.parallel-future" class="reference">

 <h1 class="title">The parallel\Future class</h1>
 

 <div class="partintro"><p class="verinfo">(0.8.0)</p>

  <div class="section">
    <h2 class="title">Futures</h2>
    <p class="para">
     A Future represents the return value or uncaught exception from a task, and exposes an API for cancellation.
    </p>
     <div class="example" id="example-3314">
      <p><strong>示例 #1 Example showing Future as return value</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$runtime&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">parallel</span><span style="color: #007700">\</span><span style="color: #0000BB">Runtime</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$future&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$runtime</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">run</span><span style="color: #007700">(function(){<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #DD0000">"World"</span><span style="color: #007700">;<br />});<br /></span><span style="color: #0000BB">printf</span><span style="color: #007700">(</span><span style="color: #DD0000">"Hello&nbsp;%s\n"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$future</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">());<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>以上例程的输出类似于：</p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
Hello World
</pre></div>
      </div>
     </div>
    <p class="para">
     The behaviour of a future also allows it to be used as a simple synchronization point even where the task does not return a value explicitly.
    </p>
     <div class="example" id="example-3315">
      <p><strong>示例 #2 Example showing Future as synchronization point</strong></p>
      <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$runtime&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;\</span><span style="color: #0000BB">parallel</span><span style="color: #007700">\</span><span style="color: #0000BB">Runtime</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$future&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$runtime</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">run</span><span style="color: #007700">(function(){<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"in&nbsp;child&nbsp;"</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">&lt;&nbsp;</span><span style="color: #0000BB">500</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">$i</span><span style="color: #007700">++)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">$i&nbsp;</span><span style="color: #007700">%&nbsp;</span><span style="color: #0000BB">10&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"."</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #DD0000">"&nbsp;leaving&nbsp;child"</span><span style="color: #007700">;<br />});<br /><br /></span><span style="color: #0000BB">$future</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">value</span><span style="color: #007700">();<br />echo&nbsp;</span><span style="color: #DD0000">"\nparent&nbsp;continues\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
      </div>

      <div class="example-contents"><p>以上例程的输出类似于：</p></div>
      <div class="example-contents screen">
<div class="cdata"><pre>
in child .................................................. leaving child
parent continues
</pre></div>
      </div>
     </div>    
  </div>

  <div class="section" id="parallel-future.synopsis">
   <h2 class="title">类摘要</h2>


   <div class="classsynopsis">
    <div class="ooclass"></div>


    <div class="classsynopsisinfo">
     <span class="ooclass">
      <span class="modifier">final</span>
      <span class="modifier">class</span> <strong class="classname">parallel\Future</strong>
     </span>
     {</div>

   
    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Resolution */</div>
    <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><a href="parallel-future.value.html" class="methodname">value</a></span>(): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* State */</div>
    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="parallel-future.cancelled.html" class="methodname">cancelled</a></span>(): <span class="type">bool</span></div>
<div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="parallel-future.done.html" class="methodname">done</a></span>(): <span class="type">bool</span></div>


    <div class="classsynopsisinfo classsynopsisinfo_comment">/* Cancellation */</div>
    <div class="methodsynopsis dc-description"><span class="modifier">public</span> <span class="methodname"><a href="parallel-future.cancel.html" class="methodname">cancel</a></span>(): <span class="type">bool</span></div>


   }</div>


  </div>

 </div>

 





























<h2>目录</h2><ul class="chunklist chunklist_reference"><li><a href="parallel-future.cancel.html">parallel\Future::cancel</a> — Cancellation</li><li><a href="parallel-future.cancelled.html">parallel\Future::cancelled</a> — State Detection</li><li><a href="parallel-future.done.html">parallel\Future::done</a> — State Detection</li><li><a href="parallel-future.value.html">parallel\Future::value</a> — Resolution</li></ul>
</div>
</div></div></body></html>